package com.crm.util;

import com.crm.model.business.Paper;
import com.crm.model.business.Question;
import com.crm.model.role.Manager;
import com.crm.model.role.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import java.util.Date;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        return configuration.buildSessionFactory(serviceRegistry);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * 测试生成表和数据
     *
     * @param args
     */
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        //测试数据学生
        /*for (int i = 1; i <= 10; i++) {
            Student student = new Student();
            student.setCardNo("lisi" + i);
            student.setName("李四" + i);
            student.setPassword("lisi" + i);
            student.setPrefession("网络");
            student.setSex("男");
            session.save(student);
        }*/
        //测试数据管理员
        /*for (int i = 1; i <= 10; i++) {
            Manager manager = new Manager();
            manager.setName("王五" + i);
            manager.setUserName("wangwu" + i);
            manager.setPassword("wangwu" + i);
            session.save(manager);
        }*/


        //测试试卷数据
        /*Paper paper1 = new Paper();
        paper1.setName("Java试卷一");
        paper1.setCreateTime(new Date());
        session.save(paper1);
        Paper paper2 = new Paper();
        paper2.setName("语文试卷二");
        paper2.setCreateTime(new Date());
        session.save(paper2);
        Paper paper3 = new Paper();
        paper3.setName("数学试卷一");
        paper3.setCreateTime(new Date());
        session.save(paper3);
        Paper paper4 = new Paper();
        paper4.setName("数据库试卷一");
        paper4.setCreateTime(new Date());
        session.save(paper4);*/

        //测试题目数据
        /*Question question1 = new Question();
        question1.setAnswer("D");
        question1.setCreateTime(new Date());
        question1.setOptionA("a1");
        question1.setOptionB("$1");
        question1.setOptionC("_1");
        question1.setOptionD("11");
        question1.setSubject("下列不可作为java语言修饰符的是");
        question1.setType("1");
        question1.setPaper((Paper)session.get(Paper.class,1));
        session.save(question1);

        Question question2 = new Question();
        question2.setAnswer("A");
        question2.setCreateTime(new Date());
        question2.setOptionA("a1.java");
        question2.setOptionB("a.class");
        question2.setOptionC("a1");
        question2.setOptionD("都可以");
        question2.setSubject("有一段java应用程序，它的主类名是a1，那么保存？");
        question2.setType("1");
        question2.setPaper((Paper)session.get(Paper.class,1));
        session.save(question2);

        Question question3 = new Question();
        question3.setAnswer("A,B");
        question3.setCreateTime(new Date());
        question3.setOptionA("String []a");
        question3.setOptionB("String a[]");
        question3.setOptionC("char a[][]");
        question3.setOptionD("String a[10]");
        question3.setSubject("下面正确声明一个一维数组的是？");
        question3.setType("2");
        question3.setPaper((Paper)session.get(Paper.class,1));
        session.save(question3);

        Question question4 = new Question();
        question4.setAnswer("A,D");
        question4.setCreateTime(new Date());
        question4.setOptionA("在java中只允许单继承。");
        question4.setOptionB("在java中一个类只能实现一个接口。");
        question4.setOptionC("在java中一个类不能同时继承一个类和实现一个接口。");
        question4.setOptionD("java的单一继承使代码更可靠。");
        question4.setSubject("下面关于继承的叙述哪些是正确的？");
        question4.setType("2");
        question4.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question4);

        Question question5 = new Question();
        question5.setAnswer("C");
        question5.setCreateTime(new Date());
        question5.setOptionA("一个子类可以有多个父类，一个父类也可以有多个子类。");
        question5.setOptionB("一个子类可以有多个父类，但一个父类只可以有一个子类。");
        question5.setOptionC("一个子类可以有一个父类，但一个父类可以有多个子类。");
        question5.setOptionD("上述说法都不对。");
        question5.setSubject("在Java中？");
        question5.setType("1");
        question5.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question5);

        Question question6 = new Question();
        question6.setAnswer("A,D");
        question6.setCreateTime(new Date());
        question6.setOptionA("包的声明必须是源文件的第一句代码。");
        question6.setOptionB("包的声明必须紧跟在import语句的后面。");
        question6.setOptionC("只有公共类才能放在包中。");
        question6.setOptionD("可以将多个源文件中的类放在同一个包中。");
        question6.setSubject("可以将多个源文件中的类放在同一个包中？");
        question6.setType("2");
        question6.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question6);

        Question question7 = new Question();
        question7.setAnswer("C");
        question7.setCreateTime(new Date());
        question7.setOptionA("Java是跨平台的编程语言。");
        question7.setOptionB("Java支持分布式计算。");
        question7.setOptionC("Java是面向过程的编程语言。");
        question7.setOptionD("Java是面向对象的编程语言。");
        question7.setSubject("下列关于Java语言的特点，描述错误的是？");
        question7.setType("1");
        question7.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question7);

        Question question8 = new Question();
        question8.setAnswer("A");
        question8.setCreateTime(new Date());
        question8.setOptionA("1。");
        question8.setOptionB("2。");
        question8.setOptionC("3。");
        question8.setOptionD("4。");
        question8.setSubject("描述错误的是8？");
        question8.setType("1");
        question8.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question8);

        Question question9 = new Question();
        question9.setAnswer("A");
        question9.setCreateTime(new Date());
        question9.setOptionA("1。");
        question9.setOptionB("2。");
        question9.setOptionC("3。");
        question9.setOptionD("4。");
        question9.setSubject("描述错误的是9？");
        question9.setType("1");
        question9.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question9);

        Question question10 = new Question();
        question10.setAnswer("A");
        question10.setCreateTime(new Date());
        question10.setOptionA("1。");
        question10.setOptionB("2。");
        question10.setOptionC("3。");
        question10.setOptionD("4。");
        question10.setSubject("描述错误的是10？");
        question10.setType("1");
        question10.setPaper((Paper) session.get(Paper.class, 1));
        session.save(question10);*/

        session.getTransaction().commit();
    }
}
